<script setup>
import { ElMessage } from 'element-plus'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import LoginBanner from '@/views/Login/components/LoginBanner.vue'
// 表单校验对象
const userInfo = ref({
  // account: '12056258283',
  // password: 'hm#qd@23!',
  account: 'ai_labs',
  password: 'ai_labs',
  agree: true
})
// 规则数据对象
const rules = {
  account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 16, message: '长度在 8 到 16 个字符', trigger: 'blur' }
  ],
  agree: [
    {
      validator: (rule, val, callback) => {
        return val ? callback() : new Error('请先同意协议')
      }
    }
  ]
}
// 登录逻辑
const formRef = ref()
const router = useRouter()
const userStore = useUserStore()
const doLogin = () => {
  // 提交表单进行预校验
  formRef.value.validate(async (valid) => {
    // console.log(valid) // true
    if (valid) {
      // 通过校验
      const { account, password } = userInfo.value
      await userStore.getUserInfo({ account, password })
      // 提示用户
      ElMessage.success('登录成功')
      // 跳转首页
      router.replace({ path: '/' })
    }
  })
}
</script>

<template>
  <div>
    <header class="login-header">
      <div class="container m-top-20">
        <h1 class="logo"> 
          <RouterLink to="/">智美衣阁</RouterLink>
        </h1>
        <RouterLink class="entry" to="/">
          进入店铺
          <i class="iconfont icon-angle-right"></i>
          <i class="iconfont icon-angle-right"></i>
        </RouterLink>
      </div>
    </header>
    <section class="login-section">
      <div class="container">
        <div class="logo">
          <img alt="logo" src="@/assets/images/logo.png" height="60"/>
        </div>
        <LoginBanner />
      </div>
      <div class="wrapper">
        <nav>
          <a href="javascript:;">账号登录</a>
        </nav>
        <div class="account-box">
          <div class="form">
            <el-form
              label-position="right"
              label-width="60px"
              status-icon
              ref="formRef"
              :model="userInfo"
              :rules="rules"
            >
              <el-form-item label="账号" prop="account">
                <el-input v-model="userInfo.account" />
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input v-model="userInfo.password" type="password"/>
              </el-form-item>
              <el-form-item label-width="22px">
                <el-checkbox size="large" v-model="userInfo.agree">
                  我已同意隐私条款和服务条款
                </el-checkbox>
              </el-form-item>
              <el-button type="primary" size="large" class="subBtn" @click="doLogin">点击登录</el-button>
            </el-form>
          </div>
        </div>
      </div>
    </section>

    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="javascript:;">关于我们</a>
          <a href="javascript:;">帮助中心</a>
          <a href="javascript:;">售后服务</a>
          <a href="javascript:;">商务合作</a>
          <a href="javascript:;">搜索推荐</a>
          <a href="javascript:;">友情链接</a>
        </p>
        <p>CopyRight &copy; AI-Labs</p>
      </div>
    </footer>
  </div>
</template>

<style scoped lang="scss">
.login-header {
  background: #fff;
  // border-bottom: 1px solid #e4e4e4;

  .container {
    width: 900px;
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }

  .logo {
    width: 200px;

    a {
      display: block;
      height: 120px;
      width: 100%;
      text-indent: -9999px;
    }
  }

  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: $xtxColor;
      letter-spacing: -5px;
    }
  }
}

.login-section {
  // background: url('@/assets/images/login-bg.png') no-repeat center / cover;
  background: #fff;
  height: 500px;
  position: relative;

  .container {
    display: flex;
    height: 500px;
    border: 1px solid #dedede;
    width: 900px;
    border-radius: 10px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);

    .banner {
      width: 400px;
      // background: linear-gradient(163.85deg, #1d291d 0%, #00308f 100%);
      background: linear-gradient(163.85deg, #014e61 0%, #50f71d 100%);
    }

    .content {
      border-radius: 10px;
      position: relative;
      display: flex;
      flex: 1;
      align-items: center;
      justify-content: center;
      padding-bottom: 40px;
    }

    .logo {
      position: fixed;
      top: 20px;
      left: 20px;
      z-index: 1;
      display: inline-flex;
      align-items: center;

      &-text {
        margin-right: 4px;
        margin-left: 4px;
        color: var(--color-fill-1);
        font-size: 20px;
      }
    }
  }

  .wrapper {
    width: 380px;
    background: #fff;
    position: absolute;
    left: 45%;
    top: 54px;
    transform: translate3d(100px, 0, 0);
    // box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 30px 0 50px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~ a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

    a {
      color: $xtxColor;

      i {
        font-size: 14px;
      }
    }
  }

  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;

          &.error {
            border-color: $priceColor;
          }

          &.active,
          &:focus {
            border-color: $xtxColor;
          }
        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: $priceColor;

        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: $xtxColor;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}

.subBtn {
  background: $xtxColor;
  width: 100%;
  color: #fff;
}
</style>
